$(document).ready(function() {

    const language = $("#infoTop").text().length === 4 ? "zh" : "en";
    const tipsText1 = language == "zh" ? "確定要提交該項目嗎？" : "Are you sure you want to submit this data?";
    const tipsText2 = language == "zh" ? "數據提交成功！" : "Operating successfully!";
    const tipsText3 = language == "zh" ? "確定要移除該數據嗎？" : "Are you sure you want to remove this data?";
    const tipsText4 = language == "zh" ? "數據移除成功！" : "Operating successfully!";
    const btnText1 = language === "zh" ? "提交" : "Sub";
    const btnText2 = language === "zh" ? "移除" : "Del";

    /** *************************************************************** */
    if ($("#menuText").val() == "") {
        $("#menuBtn1").attr("disabled", true);
    }
    $("#menuText").keydown(function() {
        if (event.keyCode == 13)
            $("#menuBtn2").click();
    });
    $("#menuBtn1").click(function() {
        window.location.href = "sitelist";
    });
    $("#menuBtn2").click(function() {
        var name = $("#menuText").val().trim();
        if (name.trim() != "")
            window.location.href = "sitelist?name=" + name;
    });
    /** *************************************************************** */
    var name = $("input[name=name]").val();
    $("#tab1 thead th:eq(1)").click(function() {
        if (name == null || name == "")
            window.location.href = "sitelist?sort=name1";
        else
            window.location.href = "sitelist?name=" + name + "&sort=name1";
    });
    $("#tab1 thead th:eq(2)").click(function() {
        if (name == null || name == "")
            window.location.href = "sitelist?sort=client1";
        else
            window.location.href = "sitelist?name=" + name + "&sort=client1";
    });
    $("#tab1 thead th:eq(3)").click(function() {
        if (name == null || name == "")
            window.location.href = "sitelist?sort=slope1";
        else
            window.location.href = "sitelist?name=" + name + "&sort=slope1";
    });
    $("#tab1 thead th:eq(4)").click(function() {
        if (name == null || name == "")
            window.location.href = "sitelist?sort=standard1";
        else
            window.location.href = "sitelist?name=" + name + "&sort=standard1";
    });
    $("#tab1 thead th:eq(5)").click(function() {
        if (name == null || name == "")
            window.location.href = "sitelist?sort=userid1";
        else
            window.location.href = "sitelist?name=" + name + "&sort=userid1";
    });
    $("#tab1 thead th:eq(6)").click(function() {
        if (name == null || name == "")
            window.location.href = "sitelist?sort=date1";
        else
            window.location.href = "sitelist?name=" + name + "&sort=date1";
    });
    var sort = $("input[name=sort]").val();
    if (sort === "name") {
        $("#tab1 thead th:eq(1)").text($("#tab1 thead th:eq(1)").text() + "↑");
    } else if (sort === "name1") {
        $("#tab1 thead th:eq(1)").text($("#tab1 thead th:eq(1)").text() + "↓");
        $("#tab1 thead th:eq(1)").unbind("click");
        $("#tab1 thead th:eq(1)").click(function() {
            if (name == null || name == "")
                window.location.href = "sitelist?sort=name";
            else
                window.location.href = "sitelist?name=" + name + "&sort=name";
        });
    } else if (sort === "client") {
        $("#tab1 thead th:eq(2)").text($("#tab1 thead th:eq(2)").text() + "↑");
    } else if (sort === "client1") {
        $("#tab1 thead th:eq(2)").text($("#tab1 thead th:eq(2)").text() + "↓");
        $("#tab1 thead th:eq(2)").unbind("click");
        $("#tab1 thead th:eq(2)").click(function() {
            if (name == null || name == "")
                window.location.href = "sitelist?sort=client";
            else
                window.location.href = "sitelist?name=" + name + "&sort=client";
        });
    } else if (sort === "slope") {
        $("#tab1 thead th:eq(3)").text($("#tab1 thead th:eq(3)").text() + "↑");
    } else if (sort === "slope1") {
        $("#tab1 thead th:eq(3)").text($("#tab1 thead th:eq(3)").text() + "↓");
        $("#tab1 thead th:eq(3)").unbind("click");
        $("#tab1 thead th:eq(3)").click(function() {
            if (name == null || name == "")
                window.location.href = "sitelist?sort=slope";
            else
                window.location.href = "sitelist?name=" + name + "&sort=slope";
        });
    } else if (sort === "standard") {
        $("#tab1 thead th:eq(4)").text($("#tab1 thead th:eq(4)").text() + "↑");
    } else if (sort === "standard1") {
        $("#tab1 thead th:eq(4)").text($("#tab1 thead th:eq(4)").text() + "↓");
        $("#tab1 thead th:eq(4)").unbind("click");
        $("#tab1 thead th:eq(4)").click(function() {
            if (name == null || name == "")
                window.location.href = "sitelist?sort=standard";
            else
                window.location.href = "sitelist?name=" + name + "&sort=standard";
        });
    } else if (sort === "userid") {
        $("#tab1 thead th:eq(5)").text($("#tab1 thead th:eq(5)").text() + "↑");
    } else if (sort === "userid1") {
        $("#tab1 thead th:eq(5)").text($("#tab1 thead th:eq(5)").text() + "↓");
        $("#tab1 thead th:eq(5)").unbind("click");
        $("#tab1 thead th:eq(5)").click(function() {
            if (name == null || name == "")
                window.location.href = "sitelist?sort=userid";
            else
                window.location.href = "sitelist?name=" + name + "&sort=userid";
        });
    } else if (sort === "date") {
        $("#tab1 thead th:eq(6)").text($("#tab1 thead th:eq(6)").text() + "↑");
    } else if (sort === "date1") {
        $("#tab1 thead th:eq(6)").text($("#tab1 thead th:eq(6)").text() + "↓");
        $("#tab1 thead th:eq(6)").unbind("click");
        $("#tab1 thead th:eq(6)").click(function() {
            if (name == null || name == "")
                window.location.href = "sitelist?sort=date";
            else
                window.location.href = "sitelist?name=" + name + "&sort=date";
        });
    }
    /** *************************************************************** */
    /** 初始化表格 */
    var name = $("#menuText").val();
    $("#tab1 tbody tr").each(function(n) {
        var id = $(this).attr("id");
        $(this).find("a").attr("target", "_blank");
        /** *********************************************************** */
        if (name.trim() != "") {
            var text = $(this).find("td:eq(1) a").text();
            var font = "<font color='#f00'>" + name + "</font>";
            var expr = new RegExp(name,"gm");
            var cont = text.replace(expr, font);
            $(this).find("td:eq(1) a").html(cont);
        }
        /** *********************************************************** */
        $(this).find("img").click(function () {
            const text = $(this).prev().text();
            if ($(this).attr("src") === "/cctv/img/展开.png") {
                if ($(this).data("name") === undefined) {
                    $(this).parents("tr").after(getContext(text, id, n));
                    $(this).data("name", "已完成");
                } else
                    $("#tab1 tbody ." + n + "").show();
                $(this).attr("src", "/cctv/img/收起.png");
            } else {
                $("#tab1 tbody ." + n + "").hide();
                $(this).attr("src", "/cctv/img/展开.png");
            }
        });
        /** *********************************************************** */
        $(this).click(function() {
            $("#tab1 tbody tr:even").find("td:eq(0)").css("background-color", "#FAFAFA");
            $("#tab1 tbody tr:odd").find("td:eq(0)").css("background-color", "#EEEEEE");
            $(this).find("td:eq(0)").css("background-color", "#FFD58D");
        });
    });

    /** 撤回项目 */
    $("#tab1 tbody").on("click", "tr td input:nth-child(1)", function () {
        const id = $(this).parents("tr").attr("id");
        if (!confirm(tipsText1))
            return false;
        $(this).css("background-color", "#ccc");
        $(this).attr("disabled", true);
        if (Ajax("sitework/submit", {id: id}))
            showTips(tipsText2);
        setTimeout("location.reload()", 2000);
    });
    /** 删除项目 */
    $("#tab1 tbody").on("click", "tr td input:nth-child(2)", function () {
        const id = $(this).parents("tr").attr("id");
        if (!confirm(tipsText3))
            return false;
        $(this).css("background-color", "#ccc");
        $(this).attr("disabled", true);
        if (Ajax("sitework/remove", {id: id}))
            showTips(tipsText4);
        setTimeout("location.reload()", 2000);
    });

    /** *************************************************************** */
    /** 上一页 */
    $(".pagebtn:eq(0)").click(function() {
        var page = Number($("#page1").text()) - 1;
        if (sort == null || sort == "")
            window.location.href = "sitelist?name=" + name + "&page=" + page;
        else
            window.location.href = "sitelist?name=" + name + "&sort=" + sort + "&page=" + page;
    });
    /** 下一页 */
    $(".pagebtn:eq(1)").click(function() {
        var page = Number($("#page1").text()) + 1;
        if (sort == null || sort == "")
            window.location.href = "sitelist?name=" + name + "&page=" + page;
        else
            window.location.href = "sitelist?name=" + name + "&sort=" + sort + "&page=" + page;
    });
    var page1 = $("#page1").text();
    var page2 = $("#page2").text();
    if (page1 <= 1) {
        $(".pagebtn:eq(0)").attr("disabled", true);
        $(".pagebtn:eq(0)").css("color", "#999");
    }
    if (page1 == page2) {
        $(".pagebtn:eq(1)").attr("disabled", true);
        $(".pagebtn:eq(1)").css("color", "#999");
    }
    /** *************************************************************** */
    function getContext(name, id, no) {
        const data = Ajax("viewlist3", {name: name, id: id});
        let context = "";
        for (let i = 0; data != null && i < data.length; i++) {
            context += "<tr id='" + data[i].id + "' class='" + no + "'>";
            context += "  <td>-</td>";
            context += "  <td><a href='checkview?id=" + data[i].id + "' title='" + data[i].name + "' target='_blank'>" + data[i].name + "</a></td>";
            context += "  <td>" + data[i].client + "</td>";
            context += "  <td>" + data[i].slope + "</td>";
            context += "  <td>" + data[i].standard + "</td>";
            context += "  <td>" + data[i].user.name + "</td>";
            context += "  <td>" + data[i].date + "</td>";
            context += "  <td>";
            context += "    <input type='button' style='background-color:#2CACF1;' value='" + btnText1 + "'/>";
            context += "    <input type='button' style='background-color:#F34D4D;' value='" + btnText2 + "'/>";
            context += "  </td>";
            context += "</tr>";
        }
        return context;
    }

    function showTips(text) {
        $("#tips").show().delay(1800).hide(200);
        $("#tips").text(text);
    }
    function Ajax(url, data) {
        var result = null;
        $.ajax({
            url: url,
            data: data,
            type: "post",
            async: false,
            datatype: "json",
            success: function(data) {
                result = data;
            }
        });
        return result;
    }
});
